from rest_framework import serializers


class CustomDateTimeField(serializers.DateTimeField):

    def __init__(self, *args, format=None, **kwargs):
        self.format = format or '%Y-%m-%d %H:%M:%S'
        kwargs['required'] = False
        super().__init__(*args, **kwargs)


class CustomDepartmentStatusFiled(serializers.BooleanField):

    def __init__(self, *args, **kwargs):
        kwargs['required'] = False
        super().__init__(*args, **kwargs)

    def to_representation(self, value):
        if value:
            return '启用'
        return '停用'


class CustomFileField(serializers.FileField):

    def to_representation(self, data):
        return data and data.file.name


class CustomAvatarField(serializers.CharField):

    def to_internal_value(self, data):
        return data.replace("uploads/", "")
